import {request} from "../../request/index.js"

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    var user = wx.getStorageSync('user')
    if (user == "") {
      wx.showToast({
        title: '请先登录',
        icon: 'success',
        image: '../images/warning.png',
        duration: 2000
      })
      setTimeout(function () {
        wx.switchTab({
          url: '../login/login',
        })
      }, 2000)
    } else {
      console.log("success")
    }
  },

  onShow: function () {
    var user = wx.getStorageSync('user')
    if (user == "") {
      wx.showToast({
        title: '请先登录',
        icon: 'success',
        image: '../images/warning.png',
        duration: 2000
      })
      setTimeout(function () {
        wx.switchTab({
          url: '../login/login',
        })
      }, 2000)
    } else {
      console.log("success")
    }
  },
  getMyselfWire: function () {
    var user = wx.getStorageSync('user')
    request({
      url: "/wire/showNotInWareHouseWhereUserManager", 
      data: {
          managerName: user
      }, 
      header: {
        'content-type': 'application/json' // 默认值
      }
    })
    .then(res => {
      if (res.data.length == 0) {
        wx.showToast({
          title: '该人员没有借出接地线',
          icon: 'none',
          duration: 2000
        })
      } else {
        var notInWareHouseList = JSON.stringify(res.data)
        getApp().globalData.notInWareHouseList = notInWareHouseList
        wx.navigateTo({
          url: '../myselfWire/myselfWire?notInWareHouseList=' + notInWareHouseList,
        })
      }
    })
  }
})